package com.mlnx.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mlnx.device.dao.mysql.DeviceGroupMapper;
import com.mlnx.device.entity.Device;
import com.mlnx.device.entity.DeviceGroup;
import com.mlnx.device.pojo.vo.DeviceGroupVO;
import com.mlnx.device.service.DeviceGroupService;
import com.mlnx.device.service.DeviceService;
import com.mlnx.service.base.service.BaseService;
import com.mlnx.service.common.enums.ResponseEnum;
import com.mlnx.service.common.exception.MutilDeviceServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by amanda.shan on 2019/10/25.
 */
@Service
public class DeviceGroupServiceIml extends BaseService<DeviceGroupMapper, DeviceGroup> implements DeviceGroupService {

    @Autowired
    private DeviceService deviceService;

    @Autowired
    @Override
    public void setBaseMapper(DeviceGroupMapper baseMapper) {
        super.setBaseMapper(baseMapper);
    }


    @Override
    public void addGroup(DeviceGroup deviceGroup) {
        DeviceGroup deviceGroup1 = baseMapper.selectOne(new QueryWrapper<DeviceGroup>().eq("name", deviceGroup.getName()));
        if(deviceGroup1 != null){
            throw new MutilDeviceServiceException(ResponseEnum.ServerError.getCode(),"改群组名称已存在");
        }
        baseMapper.insert(deviceGroup);
    }

    @Override
    public void addDeviceToGroup(DeviceGroupVO deviceGroup) {
        Device device = new Device();
        device.setGroupId(deviceGroup.getGroupId());
        deviceService.update(device, new QueryWrapper<Device>().in("numeration", deviceGroup.getSpoDevice(),
                deviceGroup.getBpDevice(), deviceGroup.getEcgDevice()));
        if (deviceGroup.getPhoneDevice() != null) {
            deviceService.update(device, new QueryWrapper<Device>().eq("numeration", deviceGroup.getPhoneDevice()));
        }
    }


}

